import { _decorator, Component, Input, input, instantiate, Label, math, Node, Prefab } from 'cc';
import { GameManager, GameState } from './GameManager';
import { Enemy } from './Enemy';
const { ccclass, property } = _decorator;

@ccclass('EnemyManager')
export class EnemyManager extends Component {

    private static instance: EnemyManager = null;
    public static getInstance(): EnemyManager {
        return this.instance;
    }


    doubleClickInterval:number =  0.2;
    lastClickTime:number = 0;


    @property
    enemy_0_SpawnRate: number = 1;
    @property(Prefab)
    enemy_0: Prefab = null;

    @property
    enemy_1_SpawnRate: number = 1;
    @property(Prefab)
    enemy_1: Prefab = null;

    @property
    enemy_2_SpawnRate: number = 1;
    @property(Prefab)
    enemy_2: Prefab = null;

    @property
    enemy_3_SpawnRate: number = 1;
    @property(Prefab)
    enemy_3: Prefab = null;

    @property
    enemy_4_SpawnRate: number = 1;
    @property(Prefab)
    enemy_4: Prefab = null;

    @property
    enemy_5_SpawnRate: number = 1;
    @property(Prefab)
    enemy_5: Prefab = null;

    @property
    enemy_6_SpawnRate: number = 1;
    @property(Prefab)
    enemy_6: Prefab = null;

    @property(Node)
    enemyArray: Node[] = [];

    start() {
        EnemyManager.instance = this;



    }
    protected onLoad(): void {
        this.lastClickTime = 0;
        input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
    }

    update(deltaTime: number) {
        if(GameManager.getInstance().gameState===GameState.WIN || GameManager.getInstance().gameState===GameState.LOSE){
            this.onDestroy();
        }
    }
    onDestroy(): void {
        // this.unschedule(this.enemy0Spawn);
        this.unschedule(this.enemy1Spawn);          //取消调度一个自定义的回调任务
        // this.unschedule(this.enemy2Spawn);
        this.unschedule(this.enemy3Spawn);
        // this.unschedule(this.enemy4Spawn);
        this.unschedule(this.enemy5Spawn);
        // this.unschedule(this.enemy6Spawn);
    }

    initEnemy(){
        // this.schedule(this.enemy0Spawn,this.enemy_0_SpawnRate);
        this.schedule(this.enemy1Spawn, this.enemy_1_SpawnRate);            //使用定时器系统调度一个自定义的回调任务。 如果回调任务已调度，那么将不会重复调度它，只会更新时间间隔参数
        // this.schedule(this.enemy2Spawn,this.enemy_2_SpawnRate);
        this.schedule(this.enemy3Spawn, this.enemy_3_SpawnRate);
        // this.schedule(this.enemy4Spawn,this.enemy_4_SpawnRate);
        this.schedule(this.enemy5Spawn, this.enemy_5_SpawnRate);
        // this.schedule(this.enemy6Spawn,this.enemy_6_SpawnRate);

    }


    // enemy0Spawn() {
    //     const enemyNode = this.objectSpawn(this.enemy_0, -330, 330, 720);
    //     this.enemArray.push(enemyNode);
    // }
    enemy1Spawn() {
        const enemyNode = this.objectSpawn(this.enemy_1, -330, 330, 720);
        this.enemyArray.push(enemyNode);
    }
    
    // enemy2Spawn() {
    //     const enemyNode = this.objectSpawn(this.enemy_2, -300, 300, 840);
    //     this.enemArray.push(enemyNode);
    // }
    enemy3Spawn() {
        const enemyNode = this.objectSpawn(this.enemy_3, -290, 290, 840);
        this.enemyArray.push(enemyNode);
    }
    // enemy4Spawn() {
    //     const enemyNode = this.objectSpawn(this.enemy_4, -290, 290, 840);
    //     this.enemArray.push(enemyNode);
    // }
    enemy5Spawn() {
        const enemyNode = this.objectSpawn(this.enemy_5, -250, 250, 840);
        this.enemyArray.push(enemyNode);
    }
    // enemy6Spawn(){
    //     const enemyNode = this.objectSpawn(this.enemy_6,-250,250,840);
    //     this.enemArray.push(enemyNode);
    // }

    objectSpawn(enemyPrefab: Prefab, minX: number, maxX: number, Y: number): Node {
        const enemy = instantiate(enemyPrefab);
        this.node.addChild(enemy);
        const randomx = math.randomRangeInt(minX, maxX);
        enemy.setPosition(randomx, Y, 0);
        return enemy;
    }


    onTouchEnd(event) {
        if(GameManager.getInstance().gameState==GameState.START){
            let currentTime = Date.now();
            let timeDiff = (currentTime - this.lastClickTime) / 1000; // 转换为秒

            if (timeDiff < this.doubleClickInterval) {
                console.log("Detected a double click!");
                this.onDoubleClick();
            }

            this.lastClickTime = currentTime;
        }
    }

    onDoubleClick() {
        // 双击后执行的操作
        console.log("Double click action executed.");

        if(GameManager.getInstance().isHaveBomb()===false)return;

        GameManager.getInstance().useBomb();
        this.killEnemyArray();

    }
    killEnemyArray(){
        for(let e of this.enemyArray){
            const enemy = e.getComponent(Enemy);
            enemy.killNow();
        }
    }

    removeEnemy(n:Node){
       const index = this.enemyArray.indexOf(n);
       if(index!==-1){
            this.enemyArray.splice(index,1);
       } 
    }


}


